﻿/*
 ******************************************************************
 *      
 * 域名空间　	: GreatB2C.DALSQLServer
 * 类　名　称	: Navigator.cs
 * 类　功　能	: 数据操作实现层
 * 类　流　程    :                             
 * 运用算法　	:                             
 * 编　写　人	: 王超             
 * 编写日期　	: 2011/3/26
 * 最近更新日期　:
 * 版　　本　	: V1.0                           
 * 描　　述　	: Descript of Class                                                 
 *                                        
 ******************************************************************
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using GreatB2C.Model;
using GreatB2C.DALFactory;
using GreatB2C.DALProfile;
using GreatB2C.Config;
using GreatB2C.Utility;

namespace GreatB2C.DALSQLServer
{
    /// <summary>
    /// 类描述：
    /// </summary>
    [Serializable]
    public class Navigator : DbBase , INavigator
    {
        string strSql = string.Empty;

        #region implement methods

        /// <summary>
        /// 获取获取参数参数列表
        /// </summary>
        /// <param name="ac"></param>
        /// <returns></returns>
        private SqlParameter[] GetParameters( GreatB2C.Model.Navigator ac )
        {
            int i = 0;
            SqlParameter[] param = new SqlParameter[ 0 ];

            param[ i ] = new SqlParameter( "@am_ID" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.am_ID;

            param[ i ] = new SqlParameter( "@api_IdentID" , SqlDbType.NVarChar , 30 );
            param[ i++ ].Value = ac.api_IdentID;

            param[ i ] = new SqlParameter( "@am_ClassID" , SqlDbType.NVarChar , 12 );
            param[ i++ ].Value = ac.am_ClassID;

            param[ i ] = new SqlParameter( "@Am_position" , SqlDbType.NVarChar , 5 );
            param[ i++ ].Value = ac.Am_position;

            param[ i ] = new SqlParameter( "@am_Name" , SqlDbType.NVarChar , 20 );
            param[ i++ ].Value = ac.am_Name;

            param[ i ] = new SqlParameter( "@Am_Ename" , SqlDbType.NVarChar , 20 );
            param[ i++ ].Value = ac.Am_Ename;

            param[ i ] = new SqlParameter( "@am_FilePath" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.am_FilePath;

            param[ i ] = new SqlParameter( "@am_target" , SqlDbType.NVarChar , 20 );
            param[ i++ ].Value = ac.am_target;

            param[ i ] = new SqlParameter( "@am_ParentID" , SqlDbType.NVarChar , 12 );
            param[ i++ ].Value = ac.am_ParentID;

            param[ i ] = new SqlParameter( "@am_type" , SqlDbType.Char , 1 );
            param[ i++ ].Value = ac.am_type;

            param[ i ] = new SqlParameter( "@am_creatTime" , SqlDbType.DateTime , 8 );
            param[ i++ ].Value = ac.am_creatTime;

            param[ i ] = new SqlParameter( "@am_orderID" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.am_orderID;

            param[ i ] = new SqlParameter( "@isSys" , SqlDbType.Char , 1 );
            param[ i++ ].Value = ac.isSys;

            param[ i ] = new SqlParameter( "@siteID" , SqlDbType.NVarChar , 12 );
            param[ i++ ].Value = ac.siteID;

            param[ i ] = new SqlParameter( "@userNum" , SqlDbType.NVarChar , 15 );
            param[ i++ ].Value = ac.userNum;

            param[ i ] = new SqlParameter( "@popCode" , SqlDbType.NVarChar , 50 );
            param[ i++ ].Value = ac.popCode;

            param[ i ] = new SqlParameter( "@mainURL" , SqlDbType.NVarChar , 100 );
            param[ i++ ].Value = ac.mainURL;


            return param;
        }

        /// <summary>
        /// 检查是否有重复记录。
        /// 注意：这里要手动修改一下，需要加入次主键做为第二条件（主要是为了修改操作）
        /// </summary>
        /// <param name="objModel">对应的数据映射类</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool IsExists( GreatB2C.Model.Navigator objModel )
        {
            string Sql = "SELECT Count(*) "
                            + " FROM "
                            + "[dbo].[" + Prefix + "Navigator]"
                            + " WHERE "
                            + "	[am_ID] = @am_ID"
                //+" and 次主键=@次主键"
                            ;

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int count = Convert.ToInt32( DbHelper.ExecuteScalar( CommandType.Text , Sql , param ).ToString( ) );
                if ( count > 0 )
                {
                    return true;
                }

                return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 检索出表中所有记录	
        /// </summary>
        /// <param name="aryResult">返回对象数据集合</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public DataTable ListNavigator( )
        {
            DataTable dtData = new DataTable( );
            string Sql = "SELECT "
                            + "[am_ID],"
                            + "[api_IdentID],"
                            + "[am_ClassID],"
                            + "[Am_position],"
                            + "[am_Name],"
                            + "[Am_Ename],"
                            + "[am_FilePath],"
                            + "[am_target],"
                            + "[am_ParentID],"
                            + "[am_type],"
                            + "[am_creatTime],"
                            + "[am_orderID],"
                            + "[isSys],"
                            + "[siteID],"
                            + "[userNum],"
                            + "[popCode],"
                            + "[mainURL]"
                            + " FROM "
                            + "[dbo].[" + Prefix + "Navigator]";

            try
            {
                return DbHelper.ExecuteTable( CommandType.Text , Sql , null );
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return null;
            }
        }

        /// <summary>
        /// 根据表中主键ID查询记录信息
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <param name="DesModel">输出的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public GreatB2C.Model.Navigator ListByID( GreatB2C.Model.Navigator objModel )
        {
            string Sql = "SELECT * "
                            + " FROM "
                            + "[dbo].[" + Prefix + "Navigator]"
                            + " WHERE "
                            + "	[am_ID] = @am_ID";

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                using ( DataTable dtData = DbHelper.ExecuteTable( CommandType.Text , Sql , param ) )
                {
                    if ( dtData.Rows.Count > 0 )
                    {
                        GreatB2C.Model.Navigator modClass = new GreatB2C.Model.Navigator( );

                        modClass.am_ID = Convert.ToInt32( dtData.Rows[ 0 ][ "am_ID" ].ToString( ) );
                        modClass.api_IdentID = dtData.Rows[ 0 ][ "api_IdentID" ].ToString( );
                        modClass.am_ClassID = dtData.Rows[ 0 ][ "am_ClassID" ].ToString( );
                        modClass.Am_position = dtData.Rows[ 0 ][ "Am_position" ].ToString( );
                        modClass.am_Name = dtData.Rows[ 0 ][ "am_Name" ].ToString( );
                        modClass.Am_Ename = dtData.Rows[ 0 ][ "Am_Ename" ].ToString( );
                        modClass.am_FilePath = dtData.Rows[ 0 ][ "am_FilePath" ].ToString( );
                        modClass.am_target = dtData.Rows[ 0 ][ "am_target" ].ToString( );
                        modClass.am_ParentID = dtData.Rows[ 0 ][ "am_ParentID" ].ToString( );
                        modClass.am_type = dtData.Rows[ 0 ][ "am_type" ].ToString( );
                        modClass.am_creatTime = Convert.ToDateTime( dtData.Rows[ 0 ][ "am_creatTime" ].ToString( ) );
                        modClass.am_orderID = Convert.ToInt32( dtData.Rows[ 0 ][ "am_orderID" ].ToString( ) );
                        modClass.isSys = dtData.Rows[ 0 ][ "isSys" ].ToString( );
                        modClass.siteID = dtData.Rows[ 0 ][ "siteID" ].ToString( );
                        modClass.userNum = dtData.Rows[ 0 ][ "userNum" ].ToString( );
                        modClass.popCode = dtData.Rows[ 0 ][ "popCode" ].ToString( );
                        modClass.mainURL = dtData.Rows[ 0 ][ "mainURL" ].ToString( );

                        return modClass;
                    }
                }

                return null;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return null;
            }
        }

        /// <summary>
        /// 新增一条新记录到表中
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Insert( GreatB2C.Model.Navigator objModel )
        {
            string Sql = "INSERT INTO "
                        + "[dbo].[" + Prefix + "Navigator]"
                        + "("
                        + "[api_IdentID],"
                        + "[am_ClassID],"
                        + "[Am_position],"
                        + "[am_Name],"
                        + "[Am_Ename],"
                        + "[am_FilePath],"
                        + "[am_target],"
                        + "[am_ParentID],"
                        + "[am_type],"
                        + "[am_creatTime],"
                        + "[am_orderID],"
                        + "[isSys],"
                        + "[siteID],"
                        + "[userNum],"
                        + "[popCode],"
                            + "[mainURL]"
                        + ") VALUES ("
                        + "@api_IdentID,"
                        + "@am_ClassID,"
                        + "@Am_position,"
                        + "@am_Name,"
                        + "@Am_Ename,"
                        + "@am_FilePath,"
                        + "@am_target,"
                        + "@am_ParentID,"
                        + "@am_type,"
                        + "@am_creatTime,"
                        + "@am_orderID,"
                        + "@isSys,"
                        + "@siteID,"
                        + "@userNum,"
                        + "@popCode,"
                            + "@mainURL"
                        + ")";

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据主键ID更新指定记录
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Update( GreatB2C.Model.Navigator objModel )
        {
            string Sql = "UPDATE "
                        + "[dbo].[" + Prefix + "Navigator]"
                        + " SET "
                        + "[api_IdentID] = @api_IdentID,"
                        + "[am_ClassID] = @am_ClassID,"
                        + "[Am_position] = @Am_position,"
                        + "[am_Name] = @am_Name,"
                        + "[Am_Ename] = @Am_Ename,"
                        + "[am_FilePath] = @am_FilePath,"
                        + "[am_target] = @am_target,"
                        + "[am_ParentID] = @am_ParentID,"
                        + "[am_type] = @am_type,"
                        + "[am_creatTime] = @am_creatTime,"
                        + "[am_orderID] = @am_orderID,"
                        + "[isSys] = @isSys,"
                        + "[siteID] = @siteID,"
                        + "[userNum] = @userNum,"
                        + "[popCode] = @popCode,"
                            + "[mainURL] = @mainURL"
                        + " WHERE "
                        + "[am_ID] = @am_ID"
                        ;

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除指定记录
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Delete( GreatB2C.Model.Navigator objModel )
        {
            string Sql = "DELETE FROM "
                            + "[dbo].[" + Prefix + "Navigator]"
                            + " WHERE "
                            + "[am_ID] = @am_ID"
                            ;

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据ID列表批量删除记录
        /// 数据格式：1,2,3,4,5
        /// </summary>
        /// <param name="strList">ID列表</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool DeleteList( string strList )
        {
            string Sql = "DELETE FROM "
                            + "[dbo].[" + Prefix + "Navigator]"
                            + " WHERE "
                            + "[am_ID] in ('" + strList + "')"
                            ;

            try
            {
                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , null );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        #endregion

        /// <summary>
        /// 获取导航根级目录
        /// </summary>
        /// <returns></returns>
        public DataTable GetRootNavi( )
        {
            DataTable dtData = new DataTable( );
            string Sql = "SELECT * "
                        + " FROM "
                        + "[dbo].[" + Prefix + "Navigator] where am_ParentID = '" + DataDefine.ModuleRoot.ToString( ) + "' order by am_OrderID";

            try
            {
                return DbHelper.ExecuteTable( CommandType.Text , Sql , null );
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return null;
            }
        }
    }
}
